package com.sunyard.manage.controller;

import com.sunyard.constant.manage.CommonConst;
import com.sunyard.dal.dto.city.CtCode;
import com.sunyard.dal.dto.city.ProCode;
import com.sunyard.dal.entity.City;
import com.sunyard.dal.entity.District;
import com.sunyard.dal.entity.Province;
import com.sunyard.manage.service.CityService;
import com.sunyard.utils.Response.BaseResult;
import com.sunyard.utils.Response.GetResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * description: CityController
 * date: 2020/1/10 10:13
 * author: JIYU
 */
@Controller
@RequestMapping("/city")
@Slf4j
public class CityController {

    @Autowired
    private CityService cityService;

    /**
     * @Description 三级联动获取所有省份
     * , produces = "text/html;charset=UTF-8"
     */
    @ResponseBody
    @RequestMapping(value = "/threeLinkGetProv")
    public BaseResult<List<Province>> threeLinkGetProv() {
        //类型
        List<Province> list = cityService.threeLinkGetProv();
        return GetResult.getResult(list);
    }

    /**
     * @Description 三级联动获取所有城市
     * , produces = "text/html;charset=UTF-8"
     */
    @ResponseBody
    @RequestMapping(value = "/threeLinkGetCity")
    public BaseResult<List<City>> threeLinkGetCity(@RequestBody ProCode proCode) {
        try {
            if (proCode == null) {
                return GetResult.getResultFail("parentCode参数不能为空");
            }
            List<City> list = cityService.threeLinkGetCity(proCode.getProCode());
            return GetResult.getResult(list);
        } catch (Exception e) {
            log.error(e.getMessage());
            return GetResult.getResultErr();
        }
    }

    /**
     * @Description 三级联动获取所有区
     * , produces = "text/html;charset=UTF-8"
     */
    @ResponseBody
    @RequestMapping(value = "/threeLinkGetDist")
    public BaseResult<List<District>> threeLinkGetDist(@RequestBody CtCode ctCode) {
        try {
            if (ctCode == null) {
                return GetResult.getResultFail("parentCode参数不能为空");
            }
            List<District> list = cityService.threeLinkGetDist(ctCode.getCtCode());
            return GetResult.getResult(list);
        } catch (Exception e) {
            log.error(e.getMessage());
            return GetResult.getResultErr();
        }
    }

}
